package com.client;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

import com.client.answers.Answer;

public class GUIMonitor {

	public static final int				JOIN_OR_CREATE_ACTION	= 0;
	public static final int				ROOM_CREATE_ACTION		= 1;
	public static final int				ROOM_JOIN_ACTION		= 2;

	private HashMap<Integer, Answer>	actionList;
	private LinkedList<Integer>			questions;
	private Map<Integer, String>		roomList;

	public GUIMonitor()
	{
		actionList = new HashMap<Integer, Answer>();
		questions = new LinkedList<Integer>();
	}

	public synchronized Map<Integer, String> getRoomList()
	{
		while (roomList == null)
		{
			try
			{
				wait();
			} catch (InterruptedException e)
			{
				e.printStackTrace();
			}
		}
		return roomList;
	}

	public synchronized void setRoomList(Map<Integer, String> list)
	{
		roomList = list;
		notifyAll();
	}

	public synchronized Answer getAnswer(int type)
	{
		if ( type == GUIMonitor.ROOM_JOIN_ACTION){
			System.out.println("Vill hämta join info");
		}
		while (actionList.get(type) == null)
		{
			if ( type == GUIMonitor.ROOM_JOIN_ACTION){
				System.out.println("Får vänta....");
			}
			try
			{
				wait();
			} catch (InterruptedException e)
			{
				e.printStackTrace();
			}
		}
		if ( type == GUIMonitor.ROOM_JOIN_ACTION){
			System.out.println("FICK DET");
		}
		return actionList.remove(type);
	}

	public synchronized void setAnswer(int type, Answer a)
	{
		actionList.put(type, a);
		notifyAll();
	}

	public synchronized int getQuestion()
	{
		while (questions.isEmpty())
		{
			try
			{
				wait();
			} catch (InterruptedException e)
			{
				e.printStackTrace();
			}
		}
		return questions.removeLast();
	}

	public synchronized void ask(int type)
	{
		questions.addFirst(type);
		notifyAll();
	}

}
